package com.brayan.webapp.repository;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.brayan.webapp.model.PubCategory;

@Stateless
public class PubCategoryRepository {
	@PersistenceContext
	EntityManager em;
	
	
	public PubCategory getPubCategoryById(Long pubCategoryId){
		PubCategory pubCat = em.find(PubCategory.class, pubCategoryId);
		return pubCat;
	}
	
	public List<PubCategory> getAllActivePubCategories(){
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<PubCategory> criteriaQuery = builder.createQuery(PubCategory.class);
		Root<PubCategory> rootPub = criteriaQuery.from(PubCategory.class);
		criteriaQuery.select(rootPub);

		TypedQuery<PubCategory> typedQuery = em.createQuery(criteriaQuery);
		List<PubCategory> pubCategoryList = typedQuery.getResultList();

		return pubCategoryList;
	}
	
	/**
	 * Return the default Pub Category. Currently its "Private".
	 * @return
	 */
	public PubCategory getDefaultPubCategory(){
		PubCategory pubCat = em.find(PubCategory.class, 1L);
		return pubCat;
	}
}
